<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="tree-card">
        <!-- 用了一个行列布局 -->
        <tree-tools :obj="companyInfo" :is-root="true" @add-dept="onAdd" />

        <!-- 树形 -->
        <el-tree :data="treeData" :props="defaultProps" :default-expand-all="true">
          <!-- <template slot-scope="{node}">
            <tree-tools :obj="node" />
          </template> -->

          <tree-tools
            slot-scope="{data}"
            :obj="data"
            @add-edit="onEdit(data)"
            @add-dept="onAdd"
          />

        </el-tree>
      </el-card>

    </div>
    <!-- 显示弹出层 -->
    <add-dept
      ref="depts"
      v-model="dialogVisible"
      :node="treeNode"
      @add-success="loadDepartments"
      @edit-success="loadDepartments"
    />
  </div>
</template>
<script>
import TreeTools from './components/TreeTools.vue'
import { getDepartmentsList } from '@/api/department'
import { transListToTree } from '@/utils'
import AddDept from './components/add-dept.vue'
export default {
  components: {
    TreeTools,
    AddDept
  },
  data() {
    return {
      treeData: [], // 树形结构的数据
      defaultProps: {
        label: 'name'
      },
      companyInfo: { name: '不想早起部落', manager: '负责人', id: '' }, // 公司名称
      dialogVisible: false, // 弹层默认隐藏
      treeNode: {} // 点击某部门的该部门的数据

    }
  },

  created() {
    this.loadDepartments()
  },

  methods: {
    // 查询企业的组织架构列表
    async loadDepartments() {
      const res = await getDepartmentsList()
      // console.log(res)
      this.treeData = transListToTree(res.depts, '')
    },
    // 点击添加子部门
    onAdd(data) {
      // console.log(123)
      this.dialogVisible = true
      // console.log(data)
      this.treeNode = data
    },
    // 编辑部门
    onEdit(data) {
      // console.log(data)
      // 显示弹层
      this.dialogVisible = true
      // data传给子组件只为了校验
      this.treeNode = data
      // 获取最新的数据进行渲染到表单
      this.$refs.depts.getDeptsInfo(data.id)
    }

  }
}
</script>

<style scoped lang='scss'>
.el-row{
  width: 100%;
}
</style>
